﻿// 用户找回密码

var phonumber;
var vcode;

// 点击发送验证码
$("#sendvcode").click(function (){
    phonumber = $("#phonumber").val();
    if(phonumber == "" || phonumber == null){
        alert("请输入手机号！")
    }
    else{
        // 先判断该手机号是否已注册
        $.ajax({
            type: "post",
            url: "/ysfwpt/selectUserInfoByName",
            async: false,
            data: {
                username: phonumber
            },
            success: function(data) {
                // console.log("success");
                // console.log(data);
                if(jQuery.isEmptyObject(data)){
                    alert("账号不存在！");
                }
                else{
                    // 发送验证码
                    $.ajax({
                        type: "post",
                        url: "/ysfwpt/sendMessage",
                        async: false,
                        data: {
                            phonumber: phonumber
                        },
                        success: function(data) {
                            // console.log("success");
                            console.log(data);
                            if(data.result == "FAILED"){
                                alert("请稍后再尝试！");
                            }
                        }
                    });
                }
            }
        });
    }
});

// 点击下一步
$("#next").click(function (){
    vcode = $("#vcode").val();
    if(vcode == "" || vcode == null){
        alert("请输入验证码！")
    }
    else{
        var vcodeCookie = $.cookie("vcodeCookie");
        //获取用户输入的验证码，和cookie中的验证码（base64加密后）
        //将用户输入的验证码也用base64加密，再和cookie中的验证码进行比对
        $.ajax({
            type: "post",
            url: "/ysfwpt/conformVcode",
            async: false,
            data: {
                vcode: vcode,
                vcodeCookie: vcodeCookie
            },
            success: function(data) {
                // console.log("success");
                // console.log(data);
                if(data.result == "correct"){
                    $("#forget_input").css("display","none");
                    $("#reset_input").css("display","block");
                }
                if(data.result == "error"){
                    $("#vcode").val("");
                    alert("验证码错误，请重新输入！");
                }
            }
        });
    }
});

// 点击确认
$("#complete").click(function (){
    var password1 = $("#password1").val();
    var password2 = $("#password2").val();
    if(password1 == "" || password1 == null || password2 == "" || password2 == null){
        alert("请输入密码！");
    }
    else if(password1 != password2){
        $("#password2").val("");
        alert("两次密码输入不一致！");
    }
    else{
        //根据用户名获取其用户信息
        $.ajax({
            type: "post",
            url: "/ysfwpt/selectUserInfoByName",
            async: false,
            data: {
                username: phonumber
            },
            success: function(data) {
                // console.log("success");
                console.log(data);
                var jobnumber = data.user.job_number;
                $.ajax({
                    type: "post",
                    url: "/ysfwpt/updatePSW",
                    data: {
                        job_number: jobnumber,
                        password_new2: password2
                    },
                    success: function (data) {
                        // console.log("success");
                        // console.log(data);
                        alert("密码修改成功！");
                        window.location.href = "login";
                    },
                    error: function (data) {
                        // console.log("error");
                        // console.log(data);
                    }
                });
            }
        });
    }
});